<?php
class Admin extends CI_Controller {
	function index() {
		$data['title'] = 'TuoiTreOnline Administrator Page';

		$this -> load -> view('backend/login', $data);
	}

	function login() {
		$this -> form_validation -> set_rules('username', 'Username', 'required|trim|alpha_numeric|max_length[50]|xss_clean');
		$this -> form_validation -> set_rules('password', 'Password', 'required|trim|alpha_numeric|max_length[200]|xss_clean');
		if ($this -> form_validation -> run() == FALSE) {
			$this -> load -> view('backend/login');
		} else {
			extract($_POST);

			$user_id = $this -> madmin -> login($username, $password);
			if (!$user_id) {
				//login failed error
				$this -> session -> set_flashdata('login_error', TRUE);
				redirect('admin');

			} else {
				// login in

				$this -> session -> set_userdata(array('logged_in' => TRUE, 'user_id' => $user_id));
				redirect('admin/show_adminpage', $user_id);
			}
		}
	}

	function logout() {
		$this -> session -> sess_destroy();
		redirect('admin');
	}

	function show_adminpage() {
		if ($this -> session -> userdata('logged_in')) {
			$data['title'] = 'TuoiTreOnline Administrator Page';
			$data['content'] = 'main_content';
			$this -> load -> view('backend/admin_page', $data);
		} else
			redirect('admin');
	}

	function article() {
		if ($this -> session -> userdata('logged_in')) {
			$this -> load -> library('pagination');
			$param = $this -> uri -> segment(4);
			$config['base_url'] = base_url() . 'index.php/admin/article/';
			$config['total_rows'] = $this -> mhome -> get_total_rows_news();
			$config['per_page'] = 5;
			$config['uri_segment'] = 4;
			$this -> pagination -> initialize($config);
			$data['news'] = $this -> mhome -> get_news($config['per_page'], $param);
			$data['content'] = 'article';
			$this -> load -> view('backend/admin_page', $data);
		}
		return $this -> mhome -> get_news($config['per_page'], $param);
	}

	function add_article() {
		if ($this -> session -> userdata('logged_in')) {
			//$param = $this->uri->segment(4);
			$data['backend'] = base_url() . 'index.php/admin/';
			$data['content'] = 'add_article';
			$this -> load -> view('backend/admin_page', $data);
		}
	}

	function func_save_add_article() {
		//if(true){//$this->mhome->save_add_article()){

		$image = '';
		$this -> load -> model('gallery_model');
		if ($this -> input -> post('upload')) {
			$this -> gallery_model -> do_upload();
		}
		$image_data = $this -> upload -> data();
		$image = $image_data['file_name'];

		$data = array('subCateID' => 300, 
						'title' => $this -> input -> post('title'), 
						'compendiousContent' => $this -> input -> post('compendiousContent'), 
						'content' => $this -> input -> post('content'), 
						'image' => $image, 'dateUpload' => time(), 
						'author' => $this -> input -> post('author'), 
						'keyword' => 'n/a', 'deleted' => 0, 'views' => 0, 'care' => 0);

		$this -> db -> insert('news', $data);
		//$this->show_adminpage();

		$data1['message'] = 'Article was added successfully ';
		$data1['backend'] = base_url() . 'index.php/admin/';
		$data1['content'] = 'article';
		$data1['news'] = $this -> article();
		//$this -> article();
		//}
		//else
		//	$data['message'] = 'Article was not added. Please check and try again';

	}

	function func_remove_article() {

		$param = $this -> uri -> segment(3);
		$this -> mhome -> remove_article($param);
		$this -> article();
	}

	function func_add_a_category() {
		if ($this -> mhome -> add_a_category()) {
			$data['categories'] = $this -> mhome -> get_categories();
			$data['message'] = 'category was added successfully';
			$data['content'] = 'showcategories';
			$this -> load -> view('backend/admin_page', $data);
		}
		return 0;

	}
	

	function showCat() {
		$data['categories'] = $this -> mhome -> get_categories();
		$data['backend'] = base_url() . 'index.php/admin/';
		$data['content'] = 'showcategories';
		$this -> load -> view('backend/admin_page', $data);
	}

	function func_delete_category() {
		$param = $this -> uri -> segment(3);
		if ($this -> mhome -> delete_category($param)) {
			$data['message'] = "Danh mục đã được xóa";
			$data['categories'] = $this -> mhome -> get_categories();
			$data['backend'] = base_url() . 'index.php/admin/';
			$data['content'] = 'showcategories';
			$this -> load -> view('backend/admin_page', $data);
		}
	}
	
	function func_save_edit_article(){
		$param = $this -> uri -> segment(3);
		if($this->mhome->save_edit_article($param)){
			$data['message'] = 'Article was saved successfully ';
			$data['content'] = 'article';
			$data['news'] = $this -> article();
			$this -> article();
		}
	}			
	function edit_article(){
		$param = $this -> uri -> segment(3);
		$data['article'] = $this->mhome->get_article($param);
		$data['content'] = 'edit_article';
		$this -> load -> view('backend/admin_page', $data);
	}

}
?>